package com.example.spring_seatreservation.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.spring_seatreservation.Bean.R;
import com.example.spring_seatreservation.entity.ReadingRoom;
import com.example.spring_seatreservation.mapper.ReadingRoomMapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/readingRoom")
@AllArgsConstructor
public class ReadingRoomController {

    private final ReadingRoomMapper readingRoomMapper;

    @GetMapping("getReadingRoomList")
    public List<ReadingRoom> getReadingRoomList(){
        return readingRoomMapper.getReadingRoom();
    }

    @GetMapping("getReadingRoomByFloorId")
    public List<ReadingRoom> getReadingRoomByFloorId(Long floorId){
        LambdaQueryWrapper<ReadingRoom> lambdaQueryWrapper = Wrappers.<ReadingRoom>lambdaQuery()
                .eq(ReadingRoom::getFloorId,floorId);
        return readingRoomMapper.selectList(lambdaQueryWrapper);
    }

    @PostMapping("submit")
    public Map<String, Object> submit(@RequestBody ReadingRoom readingRoom){
        if (Objects.isNull(readingRoom.getId())) {
            readingRoomMapper.insert(readingRoom);
        } else {
            readingRoomMapper.updateById(readingRoom);
        }
        return new R().ok().builder();
    }

    @PostMapping("delRoom")
    public Map<String, Object> delRoom(@RequestBody ReadingRoom readingRoom){
        int seatNum = readingRoomMapper.selectSeatByRoomId(readingRoom.getId());
        if (seatNum > 0) {
            return new R().bad("请先删除座位").builder();
        }
        readingRoomMapper.deleteById(readingRoom.getId());
        return new R().ok().builder();
    }
}
